home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / casefiddle.c < prev    next >
C/C++ Source or Header  |  1993-10-06  |  8KB  |  284 lines

  1. /* GNU Emacs case conversion functions.
  2.    Copyright (C) 1985 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "commands.h"
  25. #include "syntax.h"
  26.  
  27. enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
  28.  
  29. #include "casefiddle_p.h"
  30. #include "insdel_p.h"
  31. #include "intervals_p.h"
  32. #include "syntax_p.h"
  33. static Lisp_Object casify_object _P_((enum case_action flag, Lisp_Object obj));
  34. static _VOID_ casify_region _P_((enum case_action flag, Lisp_Object b,
  35.                                  Lisp_Object e));
  36.  
  37.  
  38. static Lisp_Object
  39. casify_object (flag, obj)
  40.      enum case_action flag;
  41.      Lisp_Object obj;
  42. {
  43.   register int i, c, len;
  44.   register int inword = flag == CASE_DOWN;
  45.  
  46.   while (1)
  47.     {
  48.       if (XTYPE (obj) == Lisp_Int)
  49.     {
  50.       c = XINT (obj);
  51.       if (c >= 0 && c <= 0400)
  52.         {
  53.           if (inword)
  54.         XFASTINT (obj) = DOWNCASE (c);
  55.           else if (!UPPERCASEP (c))
  56.         XFASTINT (obj) = UPCASE1 (c);
  57.         }
  58.       return obj;
  59.     }
  60.       if (XTYPE (obj) == Lisp_String)
  61.     {
  62.       obj = Fcopy_sequence (obj);
  63.       len = XSTRING (obj)->size;
  64.       for (i = 0; i < len; i++)
  65.         {
  66.           c = XSTRING (obj)->data[i];
  67.           if (inword)
  68.         c = DOWNCASE (c);
  69.           else if (!UPPERCASEP (c))
  70.         c = UPCASE1 (c);
  71.           XSTRING (obj)->data[i] = c;
  72.           if (flag == CASE_CAPITALIZE)
  73.         inword = SYNTAX (c) == Sword;
  74.         }
  75.       return obj;
  76.     }
  77.       obj = wrong_type_argument (Qchar_or_string_p, obj);
  78.     }
  79. }
  80.  
  81. DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0,
  82.   "Convert argument to upper case and return that.\n\
  83. The argument may be a character or string.  The result has the same type.\n\
  84. The argument object is not altered.  See also `capitalize'.")
  85.   (obj)
  86.      Lisp_Object obj;
  87. {
  88.   return casify_object (CASE_UP, obj);
  89. }
  90.  
  91. DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0,
  92.   "Convert argument to lower case and return that.\n\
  93. The argument may be a character or string.  The result has the same type.\n\
  94. The argument object is not altered.")
  95.   (obj)
  96.      Lisp_Object obj;
  97. {
  98.   return casify_object (CASE_DOWN, obj);
  99. }
  100.  
  101. DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
  102.   "Convert argument to capitalized form and return that.\n\
  103. This means that each word's first character is upper case\n\
  104. and the rest is lower case.\n\
  105. The argument may be a character or string.  The result has the same type.\n\
  106. The argument object is not altered.")
  107.   (obj)
  108.      Lisp_Object obj;
  109. {
  110.   return casify_object (CASE_CAPITALIZE, obj);
  111. }
  112.  
  113. /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
  114.    b and e specify range of buffer to operate on. */
  115.  
  116. static _VOID_
  117. casify_region (flag, b, e)
  118.      enum case_action flag;
  119.      Lisp_Object b, e;
  120. {
  121.   register int i;
  122.   register int c;
  123.   register int inword = flag == CASE_DOWN;
  124.  
  125.   if (EQ (b, e))
  126.     /* Not modifying because nothing marked */
  127.     return;
  128.  
  129.   validate_region (&b, &e);
  130.   modify_region (current_buffer, XFASTINT (b), XFASTINT (e));
  131.   record_change (XFASTINT (b), XFASTINT (e) - XFASTINT (b));
  132.  
  133.   for (i = XFASTINT (b); i < XFASTINT (e); i++)
  134.     {
  135.       c = FETCH_CHAR (i);
  136.       if (inword && flag != CASE_CAPITALIZE_UP)
  137.     c = DOWNCASE (c);
  138.       else if (!UPPERCASEP (c)
  139.            && (!inword || flag != CASE_CAPITALIZE_UP))
  140.     c = UPCASE1 (c);
  141.       FETCH_CHAR (i) = c;
  142.       if ((int) flag >= (int) CASE_CAPITALIZE)
  143.     inword = SYNTAX (c) == Sword;
  144.     }
  145.  
  146.   signal_after_change (XFASTINT (b),
  147.                XFASTINT (e) - XFASTINT (b), 
  148.                XFASTINT (e) - XFASTINT (b));
  149. }
  150.  
  151. DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
  152.   "Convert the region to upper case.  In programs, wants two arguments.\n\
  153. These arguments specify the starting and ending character numbers of\n\
  154. the region to operate on.  When used as a command, the text between\n\
  155. point and the mark is operated on.\n\
  156. See also `capitalize-region'.")
  157.   (b, e)
  158.      Lisp_Object b, e;
  159. {
  160.   casify_region (CASE_UP, b, e);
  161.   return Qnil;
  162. }
  163.  
  164. DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
  165.   "Convert the region to lower case.  In programs, wants two arguments.\n\
  166. These arguments specify the starting and ending character numbers of\n\
  167. the region to operate on.  When used as a command, the text between\n\
  168. point and the mark is operated on.")
  169.   (b, e)
  170.      Lisp_Object b, e;
  171. {
  172.   casify_region (CASE_DOWN, b, e);
  173.   return Qnil;
  174. }
  175.  
  176. DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r",
  177.   "Convert the region to capitalized form.\n\
  178. Capitalized form means each word's first character is upper case\n\
  179. and the rest of it is lower case.\n\
  180. In programs, give two arguments, the starting and ending\n\
  181. character positions to operate on.")
  182.   (b, e)
  183.      Lisp_Object b, e;
  184. {
  185.   casify_region (CASE_CAPITALIZE, b, e);
  186.   return Qnil;
  187. }
  188.  
  189. /* Like Fcapitalize but change only the initials.  */
  190.  
  191. Lisp_Object
  192. upcase_initials_region (b, e)
  193.      Lisp_Object b, e;
  194. {
  195.   casify_region (CASE_CAPITALIZE_UP, b, e);
  196.   return Qnil;
  197. }
  198.  
  199. Lisp_Object
  200. operate_on_word (arg)
  201.      Lisp_Object arg;
  202. {
  203.   Lisp_Object val;
  204.   int end, farend;
  205.  
  206.   CHECK_NUMBER (arg, 0);
  207.   farend = scan_words (point, XINT (arg));
  208.   if (!farend)
  209.     farend = XINT (arg) > 0 ? ZV : BEGV;
  210.  
  211.   end = point > farend ? point : farend;
  212.   SET_PT (end);
  213.   XFASTINT (val) = farend;
  214.  
  215.   return val;
  216. }
  217.  
  218. DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p",
  219.   "Convert following word (or ARG words) to upper case, moving over.\n\
  220. With negative argument, convert previous words but do not move.\n\
  221. See also `capitalize-word'.")
  222.   (arg)
  223.      Lisp_Object arg;
  224. {
  225.   Lisp_Object opoint;
  226.  
  227.   XFASTINT (opoint) = point;
  228.   casify_region (CASE_UP, opoint, operate_on_word (arg));
  229.   return Qnil;
  230. }
  231.  
  232. DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p",
  233.   "Convert following word (or ARG words) to lower case, moving over.\n\
  234. With negative argument, convert previous words but do not move.")
  235.   (arg)
  236.      Lisp_Object arg;
  237. {
  238.   Lisp_Object opoint;
  239.   XFASTINT (opoint) = point;
  240.   casify_region (CASE_DOWN, opoint, operate_on_word (arg));
  241.   return Qnil;
  242. }
  243.  
  244. DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p",
  245.   "Capitalize the following word (or ARG words), moving over.\n\
  246. This gives the word(s) a first character in upper case\n\
  247. and the rest lower case.\n\
  248. With negative argument, capitalize previous words but do not move.")
  249.   (arg)
  250.      Lisp_Object arg;
  251. {
  252.   Lisp_Object opoint;
  253.   XFASTINT (opoint) = point;
  254.   casify_region (CASE_CAPITALIZE, opoint, operate_on_word (arg));
  255.   return Qnil;
  256. }
  257.  
  258. _VOID_
  259. syms_of_casefiddle ()
  260. {
  261.   defsubr (&Supcase);
  262.   defsubr (&Sdowncase);
  263.   defsubr (&Scapitalize);
  264.   defsubr (&Supcase_region);
  265.   defsubr (&Sdowncase_region);
  266.   defsubr (&Scapitalize_region);
  267.   defsubr (&Supcase_word);
  268.   defsubr (&Sdowncase_word);
  269.   defsubr (&Scapitalize_word);
  270. }
  271.  
  272. _VOID_
  273. keys_of_casefiddle ()
  274. {
  275.   initial_define_key (control_x_map, Ctl('U'), "upcase-region");
  276.   Fput (intern ("upcase-region"), Qdisabled, Qt);
  277.   initial_define_key (control_x_map, Ctl('L'), "downcase-region");
  278.   Fput (intern ("downcase-region"), Qdisabled, Qt);
  279.  
  280.   initial_define_key (meta_map, 'u', "upcase-word");
  281.   initial_define_key (meta_map, 'l', "downcase-word");
  282.   initial_define_key (meta_map, 'c', "capitalize-word");
  283. }
  284.